package com.bcxk.controller;

import com.bcxk.common.CookieUtil;
import com.bcxk.common.Respond;
import com.bcxk.common.smsutil.RestAuthCode;
import com.bcxk.constants.Constants;
import com.bcxk.entity.dto.User;
import com.bcxk.service.UserService;
import com.bcxk.common.JwtUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author nmk
 *
 */
@Controller
@RequestMapping("user")
public class LoginController{
    @Autowired
    UserService userService;

    @RequestMapping("test")
    public String test(){
        Map<String, String> payload = new ConcurrentHashMap<>();
        payload.put("name", "张三");
        payload.put("age", "18");
        payload.put("sex", "男");
        String sign = JwtUtil.create(payload);

        return  "redirect:http://localhost:8989/login";
    }

    @PostMapping("login")
    @ResponseBody
    public Respond getUser(HttpServletResponse resp, @RequestParam(value = "username") String userName, @RequestParam(value = "password") String password) throws JsonProcessingException {
        User user = userService.getUser(userName);
        if (user == null) {
            return Respond.error("账号或密码不匹配，请重新输入");
        }
        if (!user.getPassword().equals(password)) {
            return Respond.error("账号或密码不匹配，请重新输入");
        }

        Map<String, String> payloadMap = new HashMap<>();
        //序列化
        String userPoJson = Constants.OBJECT_MAPPER.writeValueAsString(user);
        payloadMap.put("USER_INFO", userPoJson);
        String token = JwtUtil.create(payloadMap);
        System.out.println(token);
        return Respond.ok(token);
    }
    @RequestMapping("loginByPhone")
    @ResponseBody
    public Respond getUserBy(@RequestParam(value = "phone") String phone) throws JsonProcessingException {
        User user = userService.getUserByPhone(phone);
        if (null == user){
            return Respond.error("账号不存在，请先前往注册");
        }
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i=0 ; i<6 ;i++){
            buffer.append(random.nextInt(9));
        }
        RestAuthCode.testSendSms(buffer.toString(),phone );
        return Respond.ok(buffer);
    }

    @PostMapping("verify")
    @ResponseBody
    public Respond verify(String userName) {
        User user =userService.checkUserName(userName);
        if(null==user){
            //如果当前账号不存在 则可以注册 否则返回账号已存在
            return Respond.error("账号不存在！请先注册!");
        }
        return Respond.ok("账号可以使用");
    }

    @PostMapping("phoneRegist")
    @ResponseBody
    public Respond phoneRegist(String phone) {
        User user = new User();
        user.setPhone(phone);
        user.setPassword("123456");
        userService.save(user);
        return Respond.ok("注册成功,请前往登录页面登录,默认密码123456");
    }
    @PostMapping("emailRegist")
    @ResponseBody
    public Respond emailRegist(String email) {
        User user = new User();
        user.setEmail(email);
        user.setPassword("123456");
        userService.save(user);
        return Respond.ok("注册成功,请前往登录页面登录,默认密码123456");
    }

}
